package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linzp.bishe.common.Result;
import com.linzp.bishe.common.SaveAddress;
import com.linzp.bishe.entity.ResearchAchievements;
import com.linzp.bishe.mapper.ResearchAchievementsMapper;
import com.linzp.bishe.model.ResearchAchievementsModel;
import com.linzp.bishe.service.ParticipantsService;
import com.linzp.bishe.service.ResearchAchievementsService;
import com.linzp.bishe.utils.DealFile;
import com.linzp.bishe.utils.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@Service
public class ResearchAchievementsServiceImpl extends ServiceImpl<ResearchAchievementsMapper, ResearchAchievements> implements ResearchAchievementsService {
	@Autowired
	private ParticipantsService participantsService;
	@Autowired
	private SaveAddress saveAddress;
	@Autowired
	private ResearchAchievementsMapper researchAchievementsMapper;
	@Override
	public String saveMultipartFile(MultipartFile file) {
		return DealFile.saveMultipartFile(file,saveAddress.getAllAddress()+saveAddress.getCache());
	}

	@Override
	public void saveCacheToFile(String name) {
		DealFile.saveCacheToFile(saveAddress.getAllAddress()+saveAddress.getCache(),
				saveAddress.getAllAddress()+saveAddress.getResearchAchievements(),name);
		DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getCache()+"/"+name);
	}

	@Override
	public void getByIdAllResearchAchievements(Result success, Integer id, Integer current, Integer page, String searchAwardName, String searchAchievementName, String searchStatus) {
		if (searchAwardName!=null & !searchAwardName.isEmpty()){
			searchAwardName = '%'+searchAwardName+'%';
		}
		if (searchAchievementName!=null & !searchAchievementName.isEmpty()){
			searchAchievementName = '%'+searchAchievementName+'%';
		}
		success.put("tableData",researchAchievementsMapper.getByIdAllResearchAchievements(id, TableName.RESEARCH, searchAwardName,
				searchAchievementName, searchStatus,(current-1) * page,page));
		success.put("total",researchAchievementsMapper.getByIdAllResearchAchievementsCount(id, TableName.RESEARCH, searchAwardName,
				searchAchievementName, searchStatus));
		success.put("loadAddress",saveAddress.getAccessPath()+saveAddress.getResearchAchievements());
	}

	@Override
	public String updateMultipartFile(MultipartFile file, String fileName) {
		String name = DealFile.upDateMultipartFile(file, saveAddress.getAllAddress() + saveAddress.getResearchAchievements(), fileName);
		if (!name.equals(fileName)){
			DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getResearchAchievements()+"/"+fileName);
		}
		return name;
	}

	@Override
	public void deleteResearchAchievements(ResearchAchievementsModel researchAchievements) {
		ResearchAchievements achievements = researchAchievements.getResearchAchievements();
		this.removeById(achievements.getId());
		String materials = achievements.getSupportingMaterials();
		if (materials !=null){
			if(!materials.isEmpty()){
				DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getResearchAchievements()+"/"+materials);
			}
		}
		participantsService.removeListById(researchAchievements.getParticipants());
	}

	/**
	 * @Description 处理状态
	 * @Param [id, status]
	 * @return void
	 */
	@Override
	public void dealStatus(Integer id, String status) {
		UpdateWrapper<ResearchAchievements> wrapper = new UpdateWrapper<>();
		wrapper.eq("id",id);
		wrapper.set("status",status);
		this.update(wrapper);
	}

	@Override
	public boolean cheakRepeat(String element1) {
		QueryWrapper<ResearchAchievements> wrapper = new QueryWrapper<>();
		wrapper.eq("certificate_number",element1);
		return this.getOne(wrapper,false)!=null ? true:false;
	}
}
